<template>
  <div class="loginAdmin" v-loading="loginLoading">
    <el-form ref="form" :model="formData" :rules="rules" label-width="80px" class="login-form">
      <h2>后台管理登录</h2>
      <el-form-item label="账号" prop="username">
        <el-input v-model="formData.username" prefix-icon="el-icon-user" placeholder="请输入管理员账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="formData.password" placeholder="请输入密码" show-password
                  prefix-icon="el-icon-lock"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "loginAdmin",
  data() {
    return {
      // 登录加载状态
      loginLoading: false,
      formData: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    };
  },
  mounted() {
    // 存在用户信息，且为管理员信息，则跳转至首页/kunIndex
    if (this.$storage.has("token") &&
        this.$kunUtil.judgeUserType() === this.$sys.userType.ADMIN) {
      this.$router.push("/kunIndex");
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 表单验证通过后的登录逻辑
          this.$api.admin.loginAdmin(this.formData)
              .then((res) => {
                // console.log(res.data)
                // this.loginLoading = true;
                this.$storage.set("token", res.data.data.token)
                this.$message(
                    {
                      message: '登录成功',
                      type: 'success',
                      duration: 1000,
                      onClose: () => {
                        this.$cookie.set("token_expireTime","7 day",this.$sys.token_expireTime)
                        this.loginLoading = false;
                        this.$router.replace("/kunIndex");
                      },
                    }
                );
              })
              .catch(err => {
                console.log(err)
                this.loginLoading = false;
                this.$message.error('登录失败');
              });

        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    onReset() {
      this.$refs.form.resetFields();
    }
  }
};
</script>

<style scoped lang="less">
.loginAdmin {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background-image:linear-gradient(#6a85b6 0%,#bac8e0 100%);

  .login-form {
    background: white;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 400px;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 10px 20px 0 rgba(0, 0, 0, 0.15);
    }

    .el-form-item__content {
      margin-top: 10px;
    }

    .el-button {
      width: 30%;
      margin-top: 10px;
    }
  }
}
</style>
